package com.cdy.jxc_project.controller;

import com.cdy.jxc_project.domain.TGoodsUnit;
import com.cdy.jxc_project.domain.vo.TGoodsUnitVo;
import com.cdy.jxc_project.json.R;
import com.cdy.jxc_project.service.TGoodsUnitService;
import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/*
 * @Project:jxc_project
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/4/18 11:27
 * */
@RestController
@RequestMapping("/tGoodsUnit")
public class TGoodsUnitController {
    @Resource
    private TGoodsUnitService tGoodsUnitService;



    @PostMapping ("/queryAll")
    public R tGoodsUnitList(@RequestBody TGoodsUnitVo tGoodsUnitVo){
        System.out.println(tGoodsUnitVo);
        int startNum = (tGoodsUnitVo.getCurrentPage()-1)*tGoodsUnitVo.getPageSize();
        tGoodsUnitVo.setCurrentPage(startNum);

        List<TGoodsUnit> list = tGoodsUnitService.pagingQuery(tGoodsUnitVo);
        int querySize = tGoodsUnitService.querySize(tGoodsUnitVo);
        return R.ok().message("查询成功").data("tGoodsUnitList",list).data("queryNum",querySize);
    }
    @DeleteMapping("/deleteTGoodsUnit/{id}")
    public R deleteTGoodsUnit(@PathVariable Integer id){
        boolean b = tGoodsUnitService.deleteTGoodsUnit(id);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("错误");
        }

    }

    @PostMapping("/insertAndUpdateTGoodsUnit")
    public R insertTGoodsUnit(@RequestBody TGoodsUnit tGoodsUnit){
        try{
            if (tGoodsUnit.getId()==null){
                tGoodsUnitService.insertTGoodsUnit(tGoodsUnit);
                return R.ok().message("保存成功");
            }else {
                tGoodsUnitService.updateTGoodsUnit(tGoodsUnit);
                return R.ok().message("修改成功");
            }

        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("程序出错");
        }
    }
}
